package com.nanxhs.concurrent.thread.over;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * 序号生成任务器
 *
 * @author: haibin.tang
 * @date: 2021/1/27
 */
public class GenericSequenceRunner implements Runnable {

    private Set<Integer> codeList = new HashSet<>();

    @Override
    public void run() {
        while (true) {
            // 判断是否有中断标志
            if (codeList.size() > 10 && Thread.currentThread().isInterrupted()) {
                System.out.println(Thread.currentThread().getName() + "收到线程中断信号并且生成的code数量大于10，结束生成code任务");
                break;
            }
            int code = (int)(Math.random() * 10000);
            codeList.add(code);
            System.out.println(Thread.currentThread().getName() + " 生成code -->> " + code);
            LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));
        }
    }
}
